home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevfax.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.1 KB  |  282 lines

  1. /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevfax.c,v 1.2 2000/09/19 19:00:13 lpd Exp $ */
  20. /* Fax devices */
  21. #include "gdevprn.h"
  22. #include "strimpl.h"
  23. #include "scfx.h"
  24. #include "gdevfax.h"
  25.  
  26. /* The device descriptors */
  27. private dev_proc_print_page(faxg3_print_page);
  28. private dev_proc_print_page(faxg32d_print_page);
  29. private dev_proc_print_page(faxg4_print_page);
  30.  
  31. /* Define procedures that adjust the paper size. */
  32. const gx_device_procs gdev_fax_std_procs =
  33.     prn_params_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  34.              gdev_fax_get_params, gdev_fax_put_params);
  35.  
  36. #define FAX_DEVICE(dname, print_page)\
  37. {\
  38.     FAX_DEVICE_BODY(gx_device_fax, gdev_fax_std_procs, dname, print_page)\
  39. }
  40.  
  41.  
  42. const gx_device_fax gs_faxg3_device =
  43.     FAX_DEVICE("faxg3", faxg3_print_page);
  44.  
  45. const gx_device_fax gs_faxg32d_device =
  46.     FAX_DEVICE("faxg32d", faxg32d_print_page);
  47.  
  48. const gx_device_fax gs_faxg4_device =
  49.     FAX_DEVICE("faxg4", faxg4_print_page);
  50.  
  51. /* Open the device. */
  52. /* This is no longer needed: we retain it for client backward compatibility. */
  53. int
  54. gdev_fax_open(gx_device * dev)
  55. {
  56.     return gdev_prn_open(dev);
  57. }
  58.  
  59. /* Get/put the AdjustWidth parameter. */
  60. int
  61. gdev_fax_get_params(gx_device * dev, gs_param_list * plist)
  62. {
  63.     gx_device_fax *const fdev = (gx_device_fax *)dev;
  64.     int code = gdev_prn_get_params(dev, plist);
  65.     int ecode = code;
  66.  
  67.     if ((code = param_write_int(plist, "AdjustWidth", &fdev->AdjustWidth)) < 0)
  68.         ecode = code;
  69.     return ecode;
  70. }
  71. int
  72. gdev_fax_put_params(gx_device * dev, gs_param_list * plist)
  73. {
  74.     gx_device_fax *const fdev = (gx_device_fax *)dev;
  75.     int ecode = 0;
  76.     int code;
  77.     int aw = fdev->AdjustWidth;
  78.     const char *param_name;
  79.  
  80.     switch (code = param_read_int(plist, (param_name = "AdjustWidth"), &aw)) {
  81.         case 0:
  82.         if (aw >= 0 && aw <= 1)
  83.         break;
  84.         code = gs_error_rangecheck;
  85.     default:
  86.         ecode = code;
  87.         param_signal_error(plist, param_name, ecode);
  88.     case 1:
  89.         break;
  90.     }
  91.  
  92.     if (ecode < 0)
  93.     return ecode;
  94.     code = gdev_prn_put_params(dev, plist);
  95.     if (code < 0)
  96.     return code;
  97.  
  98.     fdev->AdjustWidth = aw;
  99.     return code;
  100. }
  101.  
  102. /* Initialize the stream state with a set of default parameters. */
  103. /* These select the same defaults as the CCITTFaxEncode filter, */
  104. /* except we set BlackIs1 = true. */
  105. private void
  106. gdev_fax_init_state_adjust(stream_CFE_state *ss,
  107.                const gx_device_fax *fdev,
  108.                int adjust_width)
  109. {
  110.     s_CFE_template.set_defaults((stream_state *)ss);
  111.     ss->Columns = fdev->width;
  112.     ss->Rows = fdev->height;
  113.     ss->BlackIs1 = true;
  114.     if (adjust_width > 0) {
  115.     /* Adjust the page width to a legal value for fax systems. */
  116.     if (ss->Columns >= 1680 && ss->Columns <= 1736) {
  117.         /* Adjust width for A4 paper. */
  118.         ss->Columns = 1728;
  119.     } else if (ss->Columns >= 2000 && ss->Columns <= 2056) {
  120.         /* Adjust width for B4 paper. */
  121.         ss->Columns = 2048;
  122.     }
  123.     }
  124. }
  125. void
  126. gdev_fax_init_state(stream_CFE_state *ss, const gx_device_fax *fdev)
  127. {
  128.     gdev_fax_init_state_adjust(ss, fdev, 1);
  129. }
  130. void
  131. gdev_fax_init_fax_state(stream_CFE_state *ss, const gx_device_fax *fdev)
  132. {
  133.     gdev_fax_init_state_adjust(ss, fdev, fdev->AdjustWidth);
  134. }
  135.  
  136. /*
  137.  * Print one strip with fax compression.  Fax devices call this once per
  138.  * page; TIFF devices call this once per strip.
  139.  */
  140. int
  141. gdev_fax_print_strip(gx_device_printer * pdev, FILE * prn_stream,
  142.              const stream_template * temp, stream_state * ss,
  143.              int width, int row_first, int row_end /* last + 1 */)
  144. {
  145.     gs_memory_t *mem = pdev->memory;
  146.     int code;
  147.     stream_cursor_read r;
  148.     stream_cursor_write w;
  149.     int in_size = gdev_mem_bytes_per_scan_line((gx_device *) pdev);
  150.     /*
  151.      * Because of the width adjustment for fax systems, width may
  152.      * be different from (either greater than or less than) pdev->width.
  153.      * Allocate a large enough buffer to account for this.
  154.      */
  155.     int col_size = (width * pdev->color_info.depth + 7) >> 3;
  156.     int max_size = max(in_size, col_size);
  157.     int lnum;
  158.     byte *in;
  159.     byte *out;
  160.     /* If the file is 'nul', don't even do the writes. */
  161.     bool nul = !strcmp(pdev->fname, "nul");
  162.  
  163.     /* Initialize the common part of the encoder state. */
  164.     ss->template = temp;
  165.     ss->memory = mem;
  166.     /* Now initialize the encoder. */
  167.     code = temp->init(ss);
  168.     if (code < 0)
  169.     return_error(gs_error_limitcheck);    /* bogus, but as good as any */
  170.  
  171.     /* Allocate the buffers. */
  172.     in = gs_alloc_bytes(mem, temp->min_in_size + max_size + 1,
  173.             "gdev_stream_print_page(in)");
  174. #define OUT_SIZE 1000
  175.     out = gs_alloc_bytes(mem, OUT_SIZE, "gdev_stream_print_page(out)");
  176.     if (in == 0 || out == 0) {
  177.     code = gs_note_error(gs_error_VMerror);
  178.     goto done;
  179.     }
  180.     /* Set up the processing loop. */
  181.     lnum = 0;
  182.     r.ptr = r.limit = in - 1;
  183.     w.ptr = out - 1;
  184.     w.limit = w.ptr + OUT_SIZE;
  185. #undef OUT_SIZE
  186.  
  187.     /* Process the image. */
  188.     for (lnum = row_first; ;) {
  189.     int status;
  190.  
  191.     if_debug7('w', "[w]lnum=%d r=0x%lx,0x%lx,0x%lx w=0x%lx,0x%lx,0x%lx\n", lnum,
  192.           (ulong)in, (ulong)r.ptr, (ulong)r.limit,
  193.           (ulong)out, (ulong)w.ptr, (ulong)w.limit);
  194.     status = temp->process(ss, &r, &w, lnum == row_end);
  195.     if_debug7('w', "...%d, r=0x%lx,0x%lx,0x%lx w=0x%lx,0x%lx,0x%lx\n", status,
  196.           (ulong)in, (ulong)r.ptr, (ulong)r.limit,
  197.           (ulong)out, (ulong)w.ptr, (ulong)w.limit);
  198.     switch (status) {
  199.         case 0:        /* need more input data */
  200.         if (lnum == row_end)
  201.             goto ok;
  202.         {
  203.             uint left = r.limit - r.ptr;
  204.  
  205.             memcpy(in, r.ptr + 1, left);
  206.             gdev_prn_copy_scan_lines(pdev, lnum++, in + left, in_size);
  207.             /* Note: we use col_size here, not in_size. */
  208.             if (col_size > in_size) {
  209.             memset(in + left + in_size, 0, col_size - in_size);
  210.             }
  211.             r.limit = in + left + col_size - 1;
  212.             r.ptr = in - 1;
  213.         }
  214.         break;
  215.         case 1:        /* need to write output */
  216.         if (!nul)
  217.             fwrite(out, 1, w.ptr + 1 - out, prn_stream);
  218.         w.ptr = out - 1;
  219.         break;
  220.     }
  221.     }
  222.  
  223.   ok:
  224.     /* Write out any remaining output. */
  225.     if (!nul)
  226.     fwrite(out, 1, w.ptr + 1 - out, prn_stream);
  227.  
  228.   done:
  229.     gs_free_object(mem, out, "gdev_stream_print_page(out)");
  230.     gs_free_object(mem, in, "gdev_stream_print_page(in)");
  231.     if (temp->release)
  232.     temp->release(ss);
  233.     return code;
  234. }
  235.  
  236. /* Print a fax page.  Other fax drivers use this. */
  237. int
  238. gdev_fax_print_page(gx_device_printer * pdev, FILE * prn_stream,
  239.             stream_CFE_state * ss)
  240. {
  241.     return gdev_fax_print_strip(pdev, prn_stream, &s_CFE_template,
  242.                 (stream_state *)ss, ss->Columns,
  243.                 0, pdev->height);
  244. }
  245.  
  246. /* Print a 1-D Group 3 page. */
  247. private int
  248. faxg3_print_page(gx_device_printer * pdev, FILE * prn_stream)
  249. {
  250.     stream_CFE_state state;
  251.  
  252.     gdev_fax_init_fax_state(&state, (gx_device_fax *)pdev);
  253.     state.EndOfLine = true;
  254.     state.EndOfBlock = false;
  255.     return gdev_fax_print_page(pdev, prn_stream, &state);
  256. }
  257.  
  258. /* Print a 2-D Group 3 page. */
  259. private int
  260. faxg32d_print_page(gx_device_printer * pdev, FILE * prn_stream)
  261. {
  262.     stream_CFE_state state;
  263.  
  264.     gdev_fax_init_fax_state(&state, (gx_device_fax *)pdev);
  265.     state.K = (pdev->y_pixels_per_inch < 100 ? 2 : 4);
  266.     state.EndOfLine = true;
  267.     state.EndOfBlock = false;
  268.     return gdev_fax_print_page(pdev, prn_stream, &state);
  269. }
  270.  
  271. /* Print a Group 4 page. */
  272. private int
  273. faxg4_print_page(gx_device_printer * pdev, FILE * prn_stream)
  274. {
  275.     stream_CFE_state state;
  276.  
  277.     gdev_fax_init_fax_state(&state, (gx_device_fax *)pdev);
  278.     state.K = -1;
  279.     state.EndOfBlock = false;
  280.     return gdev_fax_print_page(pdev, prn_stream, &state);
  281. }
  282.